//
//  WorkoutModel.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/28.
//

#import "WorkoutModel.h"

@interface WorkoutModel ()

- (void)setCountByType:(UTEModeSportRecordSummary *)data;

@end

@implementation WorkoutModel

- (void)from:(UTEModeSportRecordSummary *)data {
    self.step = data.step;
    self.distance = data.distance;
    self.heart = data.heartRateType;
    self.maxHeart = data.hrABSMaxPeak;
    self.minHeart = data.hrABSMinPeak;
    self.duration = data.totalTime;
    self.sportsType = data.type;
    self.pace = data.avgPace;
    self.verSpeed = data.speed;
    self.frequency = data.stepFrequency;
    
    self.stride = data.avgStride;
    self.longestStreak = data.longestStreak;
    self.tripped = data.tripped;
    self.jumpRopeFrequency = data.jumpTimes;
    self.swimType = data.swimType;
    self.swimPoolLength = data.swimPoolLength;
    self.tripTimes = data.tripTimes;
    self.swimAverageSwolf = data.swimAverageSwolf;
    self.altitude = data.lowestAltitude;//ios 缺失
    self.lowestAltitude = data.lowestAltitude;
    self.highestAltitude = data.highestAltitude;
    self.totalRiseHeight = data.totalRiseHeight;
    self.totalDropHeight = data.totalDropHeight;
    self.floorsUp = data.floorsUp;
    self.floorsDown = data.floorsDown;
    self.pullTime = data.pullTime;
    self.freeTime = data.freeTime;
    self.calories = data.calorie;
    // 下面两个字段ios目前没有
    self.gpsData = @"";
    self.detailData = @"";
    
    self.startTime = [DateUtils timeStampToString:data.startTime fmt:FMT_TIME_SHORT_YMSHMS];
    self.endTime = [DateUtils timeStampToString:data.endTime fmt:FMT_TIME_SHORT_YMSHMS];
    
    NSString *calendar = [DateUtils timeStampToString:data.startTime fmt:FMT_TIME_CLANDER];
    self.calendar = calendar;
    
    [self setCountByType:data];
    
    [self.storeData setObject:@(self.step) forKey:@"step"];
    [self.storeData setObject:@(self.distance) forKey:@"distance"];
    [self.storeData setObject:@(self.heart) forKey:@"heart"];
    [self.storeData setObject:@(self.maxHeart) forKey:@"max_heart"];
    [self.storeData setObject:@(self.minHeart) forKey:@"min_heart"];
    [self.storeData setObject:@(self.count) forKey:@"count"];
    [self.storeData setObject:@(self.duration) forKey:@"duration"];
    [self.storeData setObject:@(self.sportsType) forKey:@"sports_type"];
    [self.storeData setObject:@(self.pace) forKey:@"pace"];
    [self.storeData setObject:@(self.verSpeed) forKey:@"ver_speed"];
    [self.storeData setObject:@(self.frequency) forKey:@"frequency"];
    [self.storeData setObject:@(self.stride) forKey:@"stride"];
    [self.storeData setObject:@(self.longestStreak) forKey:@"longest_streak"];
    [self.storeData setObject:@(self.tripped) forKey:@"tripped"];
    [self.storeData setObject:@(self.jumpRopeFrequency) forKey:@"jump_rope_frequency"];
    [self.storeData setObject:@(self.swimType) forKey:@"swim_type"];
    [self.storeData setObject:@(self.swimPoolLength) forKey:@"swim_pool_length"];
    [self.storeData setObject:@(self.tripTimes) forKey:@"trip_times"];
    [self.storeData setObject:@(self.swimAverageSwolf) forKey:@"swim_average_swolf"];
//    [self.storeData setObject:@(self.altitude) forKey:@"altitude"];
    [self.storeData setObject:@(self.highestAltitude) forKey:@"highest_altitude"];
    [self.storeData setObject:@(self.lowestAltitude) forKey:@"lowest_altitude"];
    [self.storeData setObject:@(self.totalRiseHeight) forKey:@"total_rise_height"];
    [self.storeData setObject:@(self.totalDropHeight) forKey:@"total_drop_height"];
    [self.storeData setObject:@(self.floorsUp) forKey:@"floors_up"];
    [self.storeData setObject:@(self.floorsDown) forKey:@"floors_down"];
    [self.storeData setObject:@(self.pullTime) forKey:@"pull_time"];
    [self.storeData setObject:@(self.freeTime) forKey:@"free_time"];
    [self.storeData setObject:self.gpsData forKey:@"gps_data"];
    [self.storeData setObject:self.detailData forKey:@"detail_data"];
    [self.storeData setObject:@(self.calories) forKey:@"calories"];
    [self.storeData setObject:calendar forKey:@"calendar"];
    [self.storeData setObject:self.startTime forKey:@"start_time"];
    [self.storeData setObject:self.endTime forKey:@"end_time"];
}

-(void)parseGpsData:(NSArray *)data {
    if (data != nil) {
        __weak typeof(self)weakSelf = self;
        
        NSMutableArray *arr = [NSMutableArray alloc];
        for(int i = 0; i < [data count]; i++) {
            NSArray *latLng = [data objectAtIndex:i];
            [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:latLng[0], @"gpsLatitude", latLng[1], @"gpsLongitude", nil]];
        }
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[[NSArray alloc] initWithArray:arr] options:0 error: &error];
        if ([jsonData length] && error == nil) {
            weakSelf.gpsData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            [weakSelf.storeData setObject:self.gpsData forKey:@"gps_data"];
        } else {
            weakSelf.gpsData = @"";
        }
    }
}

-(void)parseDetailData:(UTEModeSportRecordDetail *)data {
    if (data != nil) {
        __weak typeof(self)weakSelf = self;
        
        NSMutableArray *details = [[NSMutableArray alloc] initWithCapacity: [data.detailDataList count]];
       
        NSInteger ts = data.startTime;
        for (UTEModeSportRecordDetailList *rec in data.detailDataList) {
            ts = ts + data.interval;
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [DateUtils timeStampToString: ts fmt:FMT_TIME_SHORT_YMSHMS], @"time",
                                        @(rec.speed), @"speed",
                                         @(rec.calorie), @"calorie",
                                         @(rec.elevation), @"elevation",
                                         @(rec.trashFrequency), @"trashFrequency",
                                         @(rec.swimEfficiency), @"swimEfficiency",
                                         @(rec.stepFrequency), @"stepFrequency",
                                         @(rec.heartRate), @"heartRate",
                                         @(rec.jumpSpeed), @"jumpSpeed",
                                         @(rec.fatBurnRate), @"fatBurnRate",
                                         @(rec.sugarBurnRate), @"sugarBurnRate",
                                         @(rec.pace), @"pace", nil];
            [details addObject:dict];
        }
        
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:details options:0 error: &error];
        if ([jsonData length] && error == nil) {
            weakSelf.detailData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            [self.storeData setObject:self.detailData forKey:@"detail_data"];
        } else {
            weakSelf.detailData = @"";
        }
    }
}

- (void)setCountByType:(UTEModeSportRecordSummary *)data {
    // 见android WorkoutType
    switch (data.type) {
        case EnumRYSDKSportType32JUMP_ROPE:
            self.count = data.jumpTimes;
            break;
            
        case EnumRYSDKSportType8SWIMMING:
            self.count = data.swimPullTimes;
            break;
            
        default:
            self.count = 0;
            break;
    }
}

@end
